home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Software / Freeware / Programare / bluej / bluejsetup-203.exe / {app} / examples / shapes / Triangle.java < prev   
Text File  |  2004-12-19  |  3KB  |  196 lines

  1. import java.awt.*;
  2.  
  3. /**
  4.  * A triangle that can be manipulated and that draws itself on a canvas.
  5.  * 
  6.  * @author    Michael Kolling and David J. Barnes
  7.  * @version 1.0  (15 July 2000)
  8.  */
  9.  
  10. public class Triangle
  11. {
  12.     private int height;
  13.     private int width;
  14.     private int xPosition;
  15.     private int yPosition;
  16.     private String color;
  17.     private boolean isVisible;
  18.  
  19.     /**
  20.      * Create a new triangle at default position with default color.
  21.      */
  22.     public Triangle()
  23.     {
  24.         height = 30;
  25.         width = 40;
  26.         xPosition = 50;
  27.         yPosition = 15;
  28.         color = "green";
  29.         isVisible = false;
  30.     }
  31.  
  32.     /**
  33.      * Make this triangle visible. If it was already visible, do nothing.
  34.      */
  35.     public void makeVisible()
  36.     {
  37.         isVisible = true;
  38.         draw();
  39.     }
  40.     
  41.     /**
  42.      * Make this triangle invisible. If it was already invisible, do nothing.
  43.      */
  44.     public void makeInvisible()
  45.     {
  46.         erase();
  47.         isVisible = false;
  48.     }
  49.     
  50.     /**
  51.      * Move the triangle a few pixels to the right.
  52.      */
  53.     public void moveRight()
  54.     {
  55.         moveHorizontal(20);
  56.     }
  57.  
  58.     /**
  59.      * Move the triangle a few pixels to the left.
  60.      */
  61.     public void moveLeft()
  62.     {
  63.         moveHorizontal(-20);
  64.     }
  65.  
  66.     /**
  67.      * Move the triangle a few pixels up.
  68.      */
  69.     public void moveUp()
  70.     {
  71.         moveVertical(-20);
  72.     }
  73.  
  74.     /**
  75.      * Move the triangle a few pixels down.
  76.      */
  77.     public void moveDown()
  78.     {
  79.         moveVertical(20);
  80.     }
  81.  
  82.     /**
  83.      * Move the triangle horizontally by 'distance' pixels.
  84.      */
  85.     public void moveHorizontal(int distance)
  86.     {
  87.         erase();
  88.         xPosition += distance;
  89.         draw();
  90.     }
  91.  
  92.     /**
  93.      * Move the triangle vertically by 'distance' pixels.
  94.      */
  95.     public void moveVertical(int distance)
  96.     {
  97.         erase();
  98.         yPosition += distance;
  99.         draw();
  100.     }
  101.  
  102.     /**
  103.      * Slowly move the triangle horizontally by 'distance' pixels.
  104.      */
  105.     public void slowMoveHorizontal(int distance)
  106.     {
  107.         int delta;
  108.  
  109.         if(distance < 0) 
  110.         {
  111.             delta = -1;
  112.             distance = -distance;
  113.         }
  114.         else 
  115.         {
  116.             delta = 1;
  117.         }
  118.  
  119.         for(int i = 0; i < distance; i++)
  120.         {
  121.             xPosition += delta;
  122.             draw();
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Slowly move the triangle vertically by 'distance' pixels.
  128.      */
  129.     public void slowMoveVertical(int distance)
  130.     {
  131.         int delta;
  132.  
  133.         if(distance < 0) 
  134.         {
  135.             delta = -1;
  136.             distance = -distance;
  137.         }
  138.         else 
  139.         {
  140.             delta = 1;
  141.         }
  142.  
  143.         for(int i = 0; i < distance; i++)
  144.         {
  145.             yPosition += delta;
  146.             draw();
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * Change the size to the new size (in pixels). Size must be >= 0.
  152.      */
  153.     public void changeSize(int newHeight, int newWidth)
  154.     {
  155.         erase();
  156.         height = newHeight;
  157.         width = newWidth;
  158.         draw();
  159.     }
  160.  
  161.     /**
  162.      * Change the color. Valid colors are "red", "yellow", "blue", "green",
  163.      * "magenta" and "black".
  164.      */
  165.     public void changeColor(String newColor)
  166.     {
  167.         color = newColor;
  168.         draw();
  169.     }
  170.  
  171.     /*
  172.      * Draw the triangle with current specifications on screen.
  173.      */
  174.     private void draw()
  175.     {
  176.         if(isVisible) {
  177.             Canvas canvas = Canvas.getCanvas();
  178.             int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };
  179.             int[] ypoints = { yPosition, yPosition + height, yPosition + height };
  180.             canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));
  181.             canvas.wait(10);
  182.         }
  183.     }
  184.  
  185.     /*
  186.      * Erase the triangle on screen.
  187.      */
  188.     private void erase()
  189.     {
  190.         if(isVisible) {
  191.             Canvas canvas = Canvas.getCanvas();
  192.             canvas.erase(this);
  193.         }
  194.     }
  195. }
  196.